package cn.wolfcode.trip.web.controller;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.QiniuCloudUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/daily")
public class DailyController {
    @Autowired
    private IDailyService service;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") DailyQueryObject qo) {
        qo.setOrderBy("schedule desc");
        PageInfo<Daily> pageInfo = service.selectForList(qo);
        model.addAttribute("result", pageInfo);
        return "daily/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Daily daily, MultipartFile img) {
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = QiniuCloudUtil.uploadQiniu(img);
            daily.setCoverUrl(url);
        }
        service.saveOrUpdate(daily);
        return "redirect:/daily/list.do";

    }

    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContent(Long id){
        DailyContent content = service.getContentById(id);
        return content;
    }

}
